<?php
/*
Plugin name: QVA Options
Plugin uri: http://themeelite.com
Author name: wiloke
Author URI : http://wiloke.com
Version: 1.0
Description: QVA theme options
*/


define('WO_ADMIN_JS', plugin_dir_url(__FILE__) . 'admin/assets/js/');
define('WO_ADMIN_IMG', plugin_dir_url(__FILE__) . 'admin/assets/images/');
define('WO_ADMIN_CSS', plugin_dir_url(__FILE__) . 'admin/assets/css/');
define('WO_ADMIN_PHP', plugin_dir_path(__FILE__) . 'admin/assets/php/');
define('WO_ADMIN_LIBS', plugin_dir_path(__FILE__) . 'admin/libs/');
define('WO_ADMIN_CONS', plugin_dir_path(__FILE__) . 'admin/configs/');

// echo WO_ADMIN_JS;    
class woThemeOptions
{
    const Key_Options = "wo_qva_themeoptions";
    public $aOptions = array();
    public $aConfigs;
    public function __construct()
    {
        add_action('admin_menu', array($this, 'wo_admin_menu'));
        add_action('wp_ajax_export_theme_options_data', array($this, 'wo_export_qva_callback'));        
        add_action('wp_ajax_import-theme-options', array($this, 'wo_import_qva_callback'));        
    }


    public function wo_import_qva_callback()
    {
        if ( check_ajax_referer('qva-secure', '_wp_nonce') && isset($_REQUEST['_wp_http_referer']) &&  preg_match("/page=qva-options/i",$_REQUEST['_wp_http_referer']) )
        {
            $data = $_POST['data_import'];
            // $data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data);
            if ( empty($data) )
            {
                $res = array("error"=>true, "mes"=>"You have successfully");
            }else{
                // $content = json_encode($data);
                $content = stripslashes($data);
                $content =  $this->qva_fix_corrupted_serialized_string($content);
                $content = unserialize($content);
                // var_export($content);
                update_option("qva_save_theme_options", $content);
                $res = array("error"=>false, "mes"=>"You have successfully");
            }
        }else{
            $res = array("error"=>true, "mes"=>"Opp! Something go error :(");
        }

        echo json_encode($res);
        die();
    }



    public function wo_get_options()
    {
        $aOptions = get_option(self::Key_Options);

        return $aOptions;
    }

    public function wo_admin_menu()
    {
    	$name = add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'qva-options', array($this, 'wo_builidng_theme_options'), '', 4);
   		
   		add_action('admin_print_scripts-'.$name, array($this, 'wo_admin_print_js'));
   		add_action('admin_print_styles-'.$name, array($this, 'wo_admin_print_style'));

   
    }

    public  function qva_fix_corrupted_serialized_string($string) 
    {
        $tmp = explode(':"', $string);
        $length = count($tmp);
        for($i = 1; $i < $length; $i++) {    
            list($string) = explode('"', $tmp[$i]);
            $str_length = strlen($string);    
            $tmp2 = explode(':', $tmp[$i-1]);
            $last = count($tmp2) - 1;    
            $tmp2[$last] = $str_length;         
            $tmp[$i-1] = join(':', $tmp2);
        }
        return join(':"', $tmp);
    } 


    public function wo_admin_print_style()
    {
    	
        wp_register_style('admin-font', 'https://www.google.com/fonts#ChoosePlace:select/Collection:Open+Sans:400,600,700,800,300italic,400italic,300');
        wp_register_style('admin-grid', WO_ADMIN_CSS . 'grid.css');
    	wp_register_style('admin-ui', 'http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css');
    	wp_register_style('admin-sb', WO_ADMIN_CSS . 'jquery.switchButton.css');
    	wp_register_style('admin-awe', WO_ADMIN_CSS . 'font-awesome.min.css');
    	wp_register_style('admin-style', WO_ADMIN_CSS . 'style.css');

        wp_enqueue_style('admin-font');
        wp_enqueue_style('admin-grid');
    	// wp_enqueue_style('admin-ui');
    	wp_enqueue_style('admin-sb');
    	// wp_enqueue_style('admin-awe');
    	wp_enqueue_style('admin-style');
    	wp_enqueue_style('wp-color-picker');
    }

    public function wo_admin_print_js()
    {
    	
    	wp_register_script('wo-jquery-cookie', WO_ADMIN_JS . 'jquery.cookie.js');

    	// wp_register_script('admin-jquery-sb', WO_ADMIN_JS . 'jquery.switchButton.js');
    	wp_register_script('themeoptions-admin-js', WO_ADMIN_JS . 'jquery.custom.js', array(), '1.0');
        // wp_register_script('themeoptions-ui-js', WO_ADMIN_JS . 'jquery-ui-widget.js', array(), '1.10.4');

        wp_enqueue_script('jquery');
        // wp_enqueue_script('themeoptions-ui-js');
        wp_enqueue_script('media-upload');
        wp_enqueue_media();
        wp_enqueue_script('wo-jquery-cookie');
        wp_enqueue_script('jquery-ui-tabs');
        wp_enqueue_script('wp-color-picker');
    	// wp_enqueue_script('admin-jquery-sb');
    	wp_enqueue_script('themeoptions-admin-js');

        wp_localize_script('themeoptions-admin-js', 'QVA_AJAX', array('ajaxurl'=>admin_url('admin-ajax.php')));
    	
    }

    public function wo_builidng_theme_options()
    {
    	$file = dirname(__FILE__) . '/admin/building-theme-options.php';

    	if ( !file_exists($file) )
    	{
    		echo 'the file does not exist' ;
    	}else{
    		include($file);
    	}
    }

    public function wo_export_qva_callback()
    {
        // userfull info http://davidwalsh.name/php-serialize-unserialize-issues
        $Option = get_option("qva_save_theme_options");
        // $Option = qva_fix_corrupted_serialized_string($Option);
        $content = serialize($Option);
        header("HTTP/1.1 200 OK");
        $file_name = "qva-options.txt";
        header('Content-Type: text/csv');
        $fsize = strlen($content);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header("Content-Disposition: attachment; filename=" . $file_name);
        header("Content-Length: ".$fsize);
        header("Expires: 0");
        header("Pragma: public");
        echo $content;
    }


    public function wo_get_custom_post_content($posttype)
    {
        if ( empty($posttype) ) die('Post type must  not empty!');

        $aPostMeta = get_posts( array('post_type'=> $posttype) );

        return $aPostMeta;
    }

}

$init = new woThemeOptions();